import {useState, useCallback} from 'react'
import { useConfigEventsApi } from '../../../../../store/hooks/useSearchPropsState';
import { useSearchUIState } from '../../../../../store/hooks/useSearchUIState';
import { useSearchUIDispatch } from '../../../../../store/hooks/useSearchUIDispatch';
import {
  getSearchHistory,
} from '../../../../../designer/service';
// 档案、全部 获取历史搜索关键字
export const useFetchHistoryData = () => {
  const { configEventsApi } = useConfigEventsApi();
  const { selectShowTab } = useSearchUIState();
  const updateUIState = useSearchUIDispatch();
  const [historyData, setHistoryData] = useState<any[]>([]);
  const fetchHistoryData = useCallback(() => {
    if (!selectShowTab?.guid) {
      return;
    }
    const params = {
      // ModuleName: selectShowTab?.guid || '',
      Size: 8,
    };
    // 历史搜索记录
    configEventsApi({
      request: getSearchHistory,
      params: params,
      keys: ['onHistoryParams', 'onHistory', 'onHistoryData'],
    }).then((res) => {
      if (res.Code == 0) {
        setHistoryData(res.Data);
        updateUIState({
          historyDataLength: res.Data.length - 1
        })
      }
    });
  }, []);
  return { fetchHistoryData, historyData, setHistoryData };
};
